package com.czhoujunlv.drug.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.czhoujunlv.drug.Bean.ClassFcation;
import com.czhoujunlv.drug.Bean.ClassItem;
import com.czhoujunlv.drug.R;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.BufferedSink;

public class ClassfcationFragment extends Fragment {

    List<String> leftList;
    List<ClassItem> RightList;
    String left[] = {"风寒感冒","消化不良","清热解毒","口腔溃疡","咽喉炎", "皮肤瘙痒","腹泻","眼疲劳","痔疮"};
    RecyclerView recy_left,recy_right;
    ClassFcation classFcation;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_classfcation, container, false);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
        leftList = new ArrayList<>();
        leftList.clear();
        for(int i =0 ;i<left.length;i++){
            leftList.add(left[i]);
        }
        Log.i("left的大小：",String.valueOf(leftList.size()));

        recy_left.setAdapter(new LeftAdapter());
        recy_left.setLayoutManager(new LinearLayoutManager(getActivity()));

        Thread thread = new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    String s = post_class("http://192.168.43.138:8080/index/getGoodByCategoryName/"+ "风寒感冒");
                    Gson gson = new Gson();
                    classFcation = new ClassFcation();
                    classFcation = gson.fromJson(s,ClassFcation.class);
                    RightList = new ArrayList<>();
                    RightList.clear();
                    for(int i=0;i<classFcation.getData().size();i++){
                        ClassItem classItem = new ClassItem();
                        classItem.img = classFcation.getData().get(i).getImg();
                        classItem.name = classFcation.getData().get(i).getName();
                        classItem.bread = classFcation.getData().get(i).getBrand();
                        RightList.add(classItem);
                    }
                } catch (IOException e) {
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        recy_right.setAdapter(new RightAdapter());
                        recy_right.setLayoutManager(new GridLayoutManager(getActivity(), 3));
                    }
                });
            }
        };
        thread.start();

    }

    private void init(){
        recy_left = getActivity().findViewById(R.id.recyclervie_left);
        recy_right = getActivity().findViewById(R.id.recyclervie_right);
    }



    int selectedPosition = 0 ;
    public class LeftAdapter extends RecyclerView.Adapter<LeftViewHolder>{


        @NonNull
        @Override
        public LeftViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_left,parent,false);
            LeftViewHolder leftViewHolder = new LeftViewHolder(view);
            return leftViewHolder;
        }
        @Override
        public void onBindViewHolder(@NonNull LeftViewHolder holder, final int position) {
            holder.title.setText(leftList.get(position));

            if (selectedPosition == position) {
                holder.tag.setVisibility(View.VISIBLE);
            } else {
                holder.tag.setVisibility(View.INVISIBLE);
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectedPosition = position;
                    Thread thread = new Thread(){
                        @Override
                        public void run() {
                            super.run();
                            try {
                                String s = post_class("http://192.168.43.138:8080/index/getGoodByCategoryName/"+ leftList.get(position));
                                Gson gson = new Gson();
                                classFcation = new ClassFcation();
                                classFcation = gson.fromJson(s,ClassFcation.class);
                                RightList = new ArrayList<>();
                                RightList.clear();
                                for(int i=0;i<classFcation.getData().size();i++){
                                    ClassItem classItem = new ClassItem();
                                    classItem.img = classFcation.getData().get(i).getImg();
                                    classItem.name = classFcation.getData().get(i).getName();
                                    classItem.bread = classFcation.getData().get(i).getBrand();
                                    RightList.add(classItem);
                                }
                            } catch (IOException e) {
                            }
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    recy_right.setAdapter(new RightAdapter());
                                    recy_right.setLayoutManager(new GridLayoutManager(getActivity(), 3));
                                }
                            });
                        }
                    };
                    thread.start();



                    notifyDataSetChanged();
                    notifyItemChanged(selectedPosition);//刷新当前点击item
                }
            });
        }

        @Override
        public int getItemCount() {
            return leftList.size();
        }
    }
    public class LeftViewHolder extends RecyclerView.ViewHolder{

        TextView title;
        ConstraintLayout tag;
        public LeftViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            tag = itemView.findViewById(R.id.constraintLayout16);
        }
    }
    public class RightAdapter extends RecyclerView.Adapter<RightViewHolder>{

        @NonNull
        @Override
        public RightViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_right,parent,false);
            RightViewHolder rightViewHolder = new RightViewHolder(view);
            return rightViewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull RightViewHolder holder, int position) {
            holder.bread.setText(RightList.get(position).bread);
            holder.name.setText(RightList.get(position).name);
            Glide.with(getActivity()).load(RightList.get(position).img).into(holder.img);
        }

        @Override
        public int getItemCount() {
            return RightList.size();
        }
    }
    public class RightViewHolder extends RecyclerView.ViewHolder{
        TextView name,bread;
        ImageView img;
        public RightViewHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.imageView19);
            name = itemView.findViewById(R.id.textView41);
            bread = itemView.findViewById(R.id.textView42);
        }
    }
    private String post_class(String url)throws IOException{
        RequestBody body = RequestBody.create(MediaType.parse("application/json"),"1");
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Response response = HeadFragment.client.newCall(request).execute();
        if (response.isSuccessful()) {
            String res = response.body().string();
            Log.i("返回分类结果：", res);
            return res;
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }
}



